package com.wl.api.controller.system;

import java.util.Objects;
import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.system.SecurityChannelAreaServicePartnerDTO;
import com.wl.api.model.system.SecurityChannelAreaServicePartner;
import com.wl.api.service.system.ISecurityChannelAreaLadderPriceService;
import com.wl.api.service.system.ISecurityChannelAreaServiceNetworkService;
import com.wl.api.service.system.ISecurityChannelAreaServicePartnerService;
import com.wl.api.service.system.ISecurityChannelAreaServiceService;
import com.wl.api.service.system.ISecurityChannelService;

/**
 * 
 * @author cj 区域ID --> 区域服务类型(feePickup上门取件费, selfDelivery 自送网点 ) -->
 *         上门取件服务合作者--> 价格阶梯 --> 自送网点地址 --> 价格阶梯
 * 
 * @version 区域服务类型 --> 上门取件服务定义-- 价格阶梯
 */
@RestController
@RequestMapping("/admin/area/servicePickup")
public class AdminAreaPickupController extends CommonController {

	@Autowired
	ISecurityChannelAreaServiceService channelAreaServiceService;
	@Autowired
	ISecurityChannelAreaServiceNetworkService channelAreaServiceNetworkService;
	@Autowired
	ISecurityChannelAreaServicePartnerService channelAreaServicePartnerService;
	@Autowired
	ISecurityChannelAreaLadderPriceService channelAreaLadderPriceService;
	@Autowired
	ISecurityChannelService channelService;

	/***
	 * 定义区域服务类型定义
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/manage", method = RequestMethod.POST)
	public JsonObject create(@RequestBody SecurityChannelAreaServicePartnerDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "", code = "";
		if (validate().or(s -> s.getId() != null).test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		} else {
			json.setData(channelAreaServicePartnerService.addServicePartner(param));
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}

	/**
	 * 修改数据
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/manage", method = RequestMethod.PUT)
	public JsonObject modify(@RequestBody SecurityChannelAreaServicePartnerDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "", error_code = "";
		if (validate().or(s -> s.getServiceId() == null).or(s -> s.getId() == null).test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if (pass) {
			json.setData(channelAreaServicePartnerService.updateServicePartner(param));
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}

	/** 只提供修改数据状态 */
	@RequestMapping(value = "/manage/{id}/{status}", method = RequestMethod.PUT)
	public JsonObject modifyStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status)
			throws Exception {
		JsonObject json = JsonObject.getInstance();
		getCommonUser();
		channelAreaServicePartnerService
				.updateById(SecurityChannelAreaServicePartner.builder().status(status).id(id).build());
		setJsonMessage(json);
		return json;
	}

	@RequestMapping(value = "/manage/{id}", method = RequestMethod.DELETE)
	public JsonObject del(@PathVariable("id") Integer id) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "", error_code = "";
		getCommonUser();

		if (pass) {
			channelAreaServicePartnerService.deleteById(id);
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}

	@RequestMapping(value = "/manage/{id}", method = RequestMethod.GET)
	public JsonObject getDetail(@PathVariable("id") Integer id) throws Exception {
		getCommonUser();
		JsonObject json = JsonObject.getInstance();
		json.setData(channelAreaServicePartnerService.getPartnerDetail(id));
		setJsonMessage(json);
		return json;
	}

	@RequestMapping(value = "/manage", method = RequestMethod.GET)
	public JsonObject getItemList(SecurityChannelAreaServicePartnerDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		getCommonUser();
		param.buildStartTime(param.getStartTime()).buildEndTime(param.getEndTime()).buildPage(param.getPage(),
				param.getPageSize());
		json.setPage(channelAreaServicePartnerService.getServicePartnerList(param));
		setJsonMessage(json);
		return json;
	}

	private Predicate<SecurityChannelAreaServicePartnerDTO> validate() {

		return s -> Objects.isNull(s.getServiceId()) || Objects.isNull(s.getPartnerId())
				|| StringUtil.isEmpty(s.getName()) || StringUtil.isEmpty(s.getLogoService())
				|| StringUtil.isEmpty(s.getDesc());

	}

}
